package com.zhou.controller;

import com.zhou.dto.dictionary.AddDictionaryDTO;
import com.zhou.dto.dictionary.UpdateDictionaryDTO;
import com.zhou.result.R;
import com.zhou.service.DictionaryService;
import com.zhou.utils.APPApplicationException;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Description : 字典
 * @Author : zyy
 * @Date: 2024/6/17  15:12
 */
@RestController
@Api(tags = "字典")
@RequestMapping("/dictionary")
public class DictionaryController {

    @Autowired
    private DictionaryService dictionaryService;

    /**
     * 分页查询
     */
    @Operation(summary = "分页查询")
    @GetMapping("query")
    public R query(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize,
                   @RequestParam String dictionaryType) {
        try {
            return R.success(dictionaryService.getList(page, pageSize,dictionaryType));
        } catch (APPApplicationException e) {
            return R.error(e.getMessage());
        }
    }

    @Operation(summary = "新增字典")
    @PostMapping("add")
    public R queryType(@RequestBody AddDictionaryDTO dto) {
        try {
            return R.success(dictionaryService.add(dto));
        } catch (APPApplicationException e) {
            return R.error(e.getMessage());
        }
    }

    @Operation(summary = "修改字典")
    @PutMapping("update")
    public R queryType(@RequestBody UpdateDictionaryDTO dto) {
        try {
            return R.success(dictionaryService.update(dto));
        } catch (APPApplicationException e) {
            return R.error(e.getMessage());
        }
    }

    /**
     * 软删除
     * @param dictionaryId
     * @return
     */
    @Operation(summary = "停用字典")
    @PutMapping("update/deactivate")
    public R queryType(@Parameter(name = "dictionaryId", description = "字典id", required = true) @RequestParam String dictionaryId) {
        try {
            return R.success(dictionaryService.deleteStatus(dictionaryId));
        } catch (APPApplicationException e) {
            return R.error(e.getMessage());
        }
    }
    /**
     * 硬删除
     */
    @Operation(summary = "删除字典")
    @DeleteMapping("delete")
    public R delete(@Parameter(name = "dictionaryId", description = "字典id", required = true) @RequestParam String dictionaryId) {
        try {
            return R.success(dictionaryService.delete(dictionaryId));
        } catch (APPApplicationException e) {
            return R.error(e.getMessage());
        }
    }
}
